package com.example.demo.others.git;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.confi.JenkinsOnlineConfi;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/3/17 0017.
 */

@Component
public class AutoPushRC {


    static Logger logger = LoggerFactory.getLogger(AutoPushRC.class);


    static String tagPrefix;
    static String msgPrefix;
    static String pushMessage = ""; //push message

    @Value("${rc_prefix_tag}")

    public void setTagPrefix(String tagPrefix) {
        AutoPushRC.tagPrefix = tagPrefix;
    }

    @Value("${rc_prefix_msg}")

    public void setMsgPrefix(String msgPrefix) {
        AutoPushRC.msgPrefix = msgPrefix;
    }


    public List<JSONObject> run(String name, String msg, String pak) throws Exception {

        pushMessage = msg;

        Map<String, String> params = new HashMap<>();

        params.put("name", name);
        params.put("pack_dir", pak);
        params.put("base_target_dir", AutoPushOnline.base_target_dir);
        params.put("tagPrefix", tagPrefix);
        params.put("msgPrefix", msgPrefix);
        params.put("pushMessage", pushMessage);


        List<JSONObject> paks = null;

        if (StringUtils.isNotBlank(pak)) {
            List<JSONObject> pags = JenkinsOnlineConfi.packages.stream().filter(p -> pak.contains(p.getString("sour_jar"))).collect(Collectors.toList());
            paks = pags;
        }
        if (paks == null || paks.size() < 1) throw new RuntimeException("no module found");
        return pushUtil.run(params, paks.get(0));
    }

    @Autowired
    AutoPushUtil pushUtil;

}
